package com.yanze.schoolinformation.view.my.person;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.AppConfig;
import com.yanze.schoolinformation.base.BaseActivity;
import com.yanze.schoolinformation.databinding.ActivityModifyBinding;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.utils.DialogUtils;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.viewModule.person.ModifyNavigator;
import com.yanze.schoolinformation.viewModule.person.ModifyViewModel;

public class ModifyActivity extends BaseActivity<ActivityModifyBinding> implements ModifyNavigator {

    private int mType;
    private ModifyViewModel viewModel;
    private RespUser.UserInfo mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        mType = getIntent().getIntExtra("modifytype", 1);
        mUser = AppConfig.getUserInfo(this);
        if (mUser == null) {
            ToastUtils.show(this,"用户数据错误，请重新登录");
        }
        initView();
        showContentView();
    }

    private void initView() {
        viewModel = new ModifyViewModel(this);
        viewModel.setmNavigator(this);
        String title = "";
        String tip = "";
        switch (mType) {
            case 1:
                title = "修改昵称";
                tip = "请输入要修改的昵称";
                break;
            case 2:
                title = "修改QQ";
                tip = "请输入QQ号";
                break;
            case 3:
                title = "修改微信";
                tip = "请输入微信号";
                break;
        }
        setTitle(title);
        bindingView.cetModify.setHint(tip);
    }

    public void modifyUserInro(View view) {
        String flag = bindingView.cetModify.getText().toString();
        if ("".equals(flag)) {
            ToastUtils.show(this,"没有要修改的信息");
            return;
        }
        if (mUser == null) {
            return;
        }
        DialogUtils.showDialog(this,"请稍候...");
        switch (mType) {
            case 1://昵称
                viewModel.modifyUserInfo(mUser.getUserid(),flag,"","");
                break;
            case 2://QQ
                viewModel.modifyUserInfo(mUser.getUserid(),"",flag,"");
                break;
            case 3://微信
                viewModel.modifyUserInfo(mUser.getUserid(),"","",flag);
                break;
        }
    }

    @Override
    public void uploadSuccess(RespUser entity) {
        DialogUtils.dismissDialog();
        ToastUtils.show(this, "修改成功");
        finish();
        AppConfig.saveUserInfo(this, entity.getData());
    }

    @Override
    public void uploadFail(String msg) {
        DialogUtils.dismissDialog();
        ToastUtils.show(this,msg);
    }

    /**
     * 入口
     * 1 ：修改昵称
     * 2 ：修改QQ
     * 3 ：修改微信
     * 待添加
     */
    public static void start(Context context, int type) {
        Intent intent = new Intent(context,ModifyActivity.class);
        intent.putExtra("modifytype", type);
        context.startActivity(intent);
    }
}
